﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		Query 
 * 
 * Created:		2013-8-28 18:59:33
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System;
using System.Data;
using Besture.Common;
using Besture.Dal;
using Besture.Model;

namespace Besture.Bll
{
    public class QueryDac : BaseFacade
    {
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable(string sql)
        {
            try
            {
                QueryDal queryDal = new QueryDal();
                // create the connection to the database
                CreateConnection();
                return queryDal.GetTable(sql);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                // close the connection with the database
                CloseConnection();
            }
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="sourceName">数据源名称</param>
        /// <returns></returns>
        public DataTable GetDataSource(string sourceName)
        {
            try
            {
                CreateConnection();
                SqlDac sqlDac = new SqlDac();
                SqlModel model = sqlDac.GetModel(sourceName);
                if(model == null)
                {
                    return null;
                }
                else
                {
                    string sql = model.SqlText;
                    QueryDal queryDal = new QueryDal();
                    // create the connection to the database
                   
                    return queryDal.GetTable(sql);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                // close the connection with the database
                CloseConnection();
            }
        }

        /// <summary>
        /// 获取查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable GetTable(string sql)
        {
            QueryDac queryDac = new QueryDac();
            return queryDac.GetDataTable(sql);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="sourceName">数据源名称</param>
        /// <returns></returns>
        public static DataTable GetSource(string sourceName)
        {
            QueryDac queryDac = new QueryDac();
            return queryDac.GetDataSource(sourceName);
        }

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int ExcuteSql(string sql)
        {
            QueryDac queryDac = new QueryDac();
            return queryDac.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string sql)
        {
            try
            {
                QueryDal queryDal = new QueryDal();
                base.CreateConnection();
                base.BeginTransaction();
                int lsRecord = 0;
                lsRecord = queryDal.ExecuteNonQuery(sql);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }
    }
}
